<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="aliasName">
      <el-input v-model="dataForm.aliasName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="domainName">
      <el-input v-model="dataForm.domainName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="type">
      <el-input v-model="dataForm.type" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="companyName">
      <el-input v-model="dataForm.companyName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="orgCode">
      <el-input v-model="dataForm.orgCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="areaCode">
      <el-input v-model="dataForm.areaCode" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="addrDetail">
      <el-input v-model="dataForm.addrDetail" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="contactPhone">
      <el-input v-model="dataForm.contactPhone" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="email">
      <el-input v-model="dataForm.email" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="mngorgName">
      <el-input v-model="dataForm.mngorgName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="mngorgLeader">
      <el-input v-model="dataForm.mngorgLeader" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="mixInfo">
      <el-input v-model="dataForm.mixInfo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="status">
      <el-input v-model="dataForm.status" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="signedByCostumerId">
      <el-input v-model="dataForm.signedByCostumerId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="energyType">
      <el-input v-model="dataForm.energyType" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          aliasName: '',
          domainName: '',
          type: '',
          companyName: '',
          orgCode: '',
          areaCode: '',
          addrDetail: '',
          contactPhone: '',
          email: '',
          mngorgName: '',
          mngorgLeader: '',
          mixInfo: '',
          createTime: '',
          status: '',
          signedByCostumerId: '',
          energyType: ''
        },
        dataRule: {
          aliasName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          domainName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          companyName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          orgCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          areaCode: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          addrDetail: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          contactPhone: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mngorgName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mngorgLeader: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          mixInfo: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          signedByCostumerId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          energyType: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/syscustomer/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.aliasName = data.syscustomer.aliasName
                this.dataForm.domainName = data.syscustomer.domainName
                this.dataForm.type = data.syscustomer.type
                this.dataForm.companyName = data.syscustomer.companyName
                this.dataForm.orgCode = data.syscustomer.orgCode
                this.dataForm.areaCode = data.syscustomer.areaCode
                this.dataForm.addrDetail = data.syscustomer.addrDetail
                this.dataForm.contactPhone = data.syscustomer.contactPhone
                this.dataForm.email = data.syscustomer.email
                this.dataForm.mngorgName = data.syscustomer.mngorgName
                this.dataForm.mngorgLeader = data.syscustomer.mngorgLeader
                this.dataForm.mixInfo = data.syscustomer.mixInfo
                this.dataForm.createTime = data.syscustomer.createTime
                this.dataForm.status = data.syscustomer.status
                this.dataForm.signedByCostumerId = data.syscustomer.signedByCostumerId
                this.dataForm.energyType = data.syscustomer.energyType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/syscustomer/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'aliasName': this.dataForm.aliasName,
                'domainName': this.dataForm.domainName,
                'type': this.dataForm.type,
                'companyName': this.dataForm.companyName,
                'orgCode': this.dataForm.orgCode,
                'areaCode': this.dataForm.areaCode,
                'addrDetail': this.dataForm.addrDetail,
                'contactPhone': this.dataForm.contactPhone,
                'email': this.dataForm.email,
                'mngorgName': this.dataForm.mngorgName,
                'mngorgLeader': this.dataForm.mngorgLeader,
                'mixInfo': this.dataForm.mixInfo,
                'createTime': this.dataForm.createTime,
                'status': this.dataForm.status,
                'signedByCostumerId': this.dataForm.signedByCostumerId,
                'energyType': this.dataForm.energyType
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
